package com.fan.configserver.controller;


import com.fan.configserver.exception.QhException;
import com.fan.configserver.exception.QhUserErrors;
import com.fan.configserver.po.User;
import com.fan.configserver.service.UserService;
import com.fan.configserver.shiro.JwtHelper;
import com.fan.configserver.vo.ResponseResult;
import com.fan.configserver.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("config")
public class LoginController {
    @Autowired
    UserService userService;


    @PostMapping("login")
    public ResponseResult login(String userName, String password) {
        ResponseResult responseResult = new ResponseResult();
        User user = userService.getUserByUserName(userName);
        if (user == null||!user.getPassword().equals(password)) {
            throw new QhException(QhUserErrors.USER_ERROR);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        String jwt = JwtHelper.createJWT(user.getUserId(), "aaa", null, null, 18000l, "huangfan");
        userVo.setToken(jwt);
        responseResult.setData(userVo);
        return responseResult;
    }


    @GetMapping("/overTime")
    public ResponseResult overTime() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(303);
        responseResult.setMsg("登录超时");
        return responseResult;
    }

    @GetMapping("/tokenIsNull")
    public ResponseResult tokenIsNull() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(101);
        responseResult.setMsg("token为空");
        return responseResult;
    }


    @GetMapping("/tokenValidate")
    public ResponseResult tokenValidate() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(202);
        responseResult.setMsg("token验证失败");
        return responseResult;
    }
}
